//
//  BCURL.swift
//  BCFoundation
//
//  Created by chun.chen on 2021/8/27.
// url 扩展

import Foundation

public extension URL {
    
    /// 兼容url特殊符号URLWithString返回nil处理
    /// - Parameter string: string包含中文等符号是 URL string会返回nil
    /// - Returns: description
    static func bc_url(_ string: String) -> URL? {
        if let url = URL(string: string) {
            return url
        }
        // 当URL初始化不成立时 尝试对string编码
        guard let encodeUrlString = string.addingPercentEncoding(withAllowedCharacters:
            .urlQueryAllowed) else {
            return nil
        }
        return URL(string: encodeUrlString)
    }
}
